import { applySponsorApi } from "@/api/financialManagement/payableManagement/paymentApproval";
import { message, Modal } from "ant-design-vue";
import { computed, createVNode, Ref } from "vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
export default function OperationHook(state: {
  emit: any;
  loading: Ref<boolean>;
  submitForm: { paymentDescription: string };
  selectMap: Ref<Base.GeneraDataType>;
  customerBillRef: Ref<any>;
  selectUnchargeOff: Ref<string>;
  applyCostList: Ref<Array<PaymentApproval.ApplyCostInfo>>;
}) {
  const { selectMap, customerBillRef, submitForm, loading, emit, selectUnchargeOff, applyCostList } = state;
  const selectRows = computed<Array<ClientWaybillManagement.ListRows>>(() => {
    return customerBillRef.value.getCheckboxRecords() || [];
  });
  const selectChangeEvent = (e: any, row: any) => {
    selectMap.value[row.id] = e.records;
  };
  const selectAllEvent = (e: any, row: any) => {
    selectMap.value[row.id] = e.records;
  };
  const expandAllEvent = () => {
    const $table = customerBillRef.value;
    if ($table) {
      $table.setAllRowExpand(true);
    }
  };

  const clearExpandEvent = () => {
    const $table = customerBillRef.value;
    if ($table) {
      $table.clearRowExpand();
    }
  };
  const submitHandle = () => {
    const { paymentDescription } = submitForm;
    const params: PaymentApproval.ApplyPatmentForm = {
      paymentAmount: Number(selectUnchargeOff.value),
      paymentDescription,
      payableStatementShipmentIdList: []
    };
    for (const key in selectMap.value) {
      const target = selectMap.value[key];
      params.payableStatementShipmentIdList = target.map((t: any) => t.id) || [];
    }
    if (!params.payableStatementShipmentIdList.length) return message.error("请先勾选费用!");
    loading.value = true;
    return applySponsorApi(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("申请成功!");
          emit("submit");
        }
      })
      .finally(() => (loading.value = false));
  };
  const allSelectHandle = () => {
    selectMap.value = {};
    for (let i = 0; i < applyCostList.value.length; i++) {
      const target = applyCostList.value[i];
      selectMap.value[target.id] = target.paymentShipmentResList || [];
    }
    Modal.confirm({
      title: "是否申请所有费用?",
      icon: createVNode(ExclamationCircleOutlined),
      onOk() {
        loading.value = true;
        const { paymentDescription } = submitForm;
        const params: PaymentApproval.ApplyPatmentForm = {
          paymentAmount: Number(selectUnchargeOff.value),
          paymentDescription,
          payableStatementShipmentIdList: []
        };

        for (const key in selectMap.value) {
          const target = selectMap.value[key];
          params.payableStatementShipmentIdList = params.payableStatementShipmentIdList.concat(target.map((t: any) => t.id)) || [];
        }
        applySponsorApi(params)
          .then((res: any) => {
            const { code } = res;
            if (code === 200) {
              message.success("操作成功!");
              emit("submit");
            }
          })
          .finally(() => (loading.value = false));
      }
    });
  };
  return {
    selectRows,
    selectChangeEvent,
    selectAllEvent,
    expandAllEvent,
    clearExpandEvent,
    submitHandle,
    allSelectHandle
  };
}
